package com.ruoyi.common.utils;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;


/**
 * @Author qiqi
 * @Date 2023/3/30 14:13
 * @Version 1.0
 */
public class SignatureGeneratorUtils {

    // 定义米字格的宽度和高度
    private static final int GRID_WIDTH = 564;
    private static final int GRID_HEIGHT = 230;

    // 定义字体和字号
    private static final String FONT_NAME = "楷体";
    private static final int FONT_SIZE = 130;

    // 定义字与字之间的间距
    private static final int CHAR_GAP = 25;

    public static void main(String[] args) {
        String username = "耿亮"; // 要签名的内容
        JSONObject signImage = createSignImage(username);


    }

    /**
     * 生成签名图片返回输入流
     *
     * @param username
     * @return
     */
    public static JSONObject createSignImage(String username) {
        if(username.length() == 2){
            username = username.replace("", " ").trim();
        }

        // 创建一个 BufferedImage 对象
        BufferedImage image = new BufferedImage(GRID_WIDTH, GRID_HEIGHT, BufferedImage.TYPE_INT_RGB);

        // 获取 Graphics2D 对象，设置画笔颜色、字体和字号
        Graphics2D g2d = image.createGraphics();
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, GRID_WIDTH, GRID_HEIGHT);
        g2d.setColor(Color.BLACK);
        Font font = new Font(FONT_NAME, Font.PLAIN, FONT_SIZE);
        g2d.setPaint(Color.LIGHT_GRAY);
        g2d.setFont(font);

        // 将签名内容按照米字格的方式绘制到 BufferedImage 对象上
        int x = CHAR_GAP;
        int y = CHAR_GAP + FONT_SIZE;
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if (c == '\n') {
                x = CHAR_GAP;
                y += FONT_SIZE + CHAR_GAP;
            } else if (c == '\t') {
                x += FONT_SIZE * 4 + CHAR_GAP;
            } else {
                g2d.drawString(String.valueOf(c), x, y);
                x += FONT_SIZE + CHAR_GAP;
            }
        }
        try {
            String filePath = RuoYiConfig.getUploadPath().concat("/").concat("singImage").concat("/") + IdUtil.getSnowflakeNextId() + ".png";
            File saveFile = new File(filePath);
            // 创建文件夹
            if (!saveFile.getParentFile().exists()) {
                saveFile.getParentFile().mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(saveFile);
            boolean isUpload = ImageIO.write(image, "PNG", fos);
            if (isUpload) {
                JSONObject resultObj = new JSONObject();
                resultObj.put("isUpload", isUpload);
                resultObj.put("filePath", filePath);
                return resultObj;
            }
            return null;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            // 释放资源
            g2d.dispose();
        }
        return null;
    }

    /**
     * 获取图片
     *
     * @param image
     * @return
     */
    private static InputStream getImageStream(BufferedImage image) {
        InputStream is = null;
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ImageOutputStream imageOutputStream = null;
        try {
            imageOutputStream = ImageIO.createImageOutputStream(bs);
            ImageIO.write(image, "png", imageOutputStream);
            is = new ByteArrayInputStream(bs.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return is;
    }

}

